
package com.idealighter.game.dictionary.dic;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.dictionary.core.DictionaryTable;
import com.idealighter.game.dictionary.domain.FoursLogPlanDomain;
import com.idealighter.game.dictionary.mapper.FoursLogPlanMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Singleton
public class FoursLogPlanDic implements DictionaryTable<FoursLogPlanDomain> {

  private FoursLogPlanMapper mapper;
  private List<FoursLogPlanDomain> list;
  private Map<Integer, FoursLogPlanDomain> map;
  private int totalWeigh = 0;

  @Inject
  public FoursLogPlanDic(FoursLogPlanMapper mapper) {
    this.mapper = mapper;
    load();
  }

  @Override
  public List<FoursLogPlanDomain> list() {

    return list;
  }

  @Override
  public Map<Integer, FoursLogPlanDomain> map() {

    return map;
  }

  @Override
  public void load() {
    AtomicInteger totalWeightTmp = new AtomicInteger(0);
    this.list = mapper.selectAll();
    Map<Integer, FoursLogPlanDomain> map = new HashMap<>();
    list.forEach(val -> {
      map.put(val.getId(), val);
      totalWeightTmp.getAndAdd(val.getProbability());
    });
    this.map = map;
    this.totalWeigh = totalWeightTmp.intValue();
  }

  @Override
  public FoursLogPlanDomain get(int id) {

    return map.get(id);
  }

  public int getTotalWeight() {
    return totalWeigh / 5;
  }
}
